package com.su.kddq.controller.user;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.su.kddq.entity.user.Order;
import com.su.kddq.entity.user.PayOrder;
import com.su.kddq.entity.user.User;
import com.su.kddq.entity.user.UserWallet;
import com.su.kddq.entity.user.vo.UserRegister;
import com.su.kddq.service.user.OrderService;
import com.su.kddq.service.user.UserService;
import com.su.kddq.utils.result.R;
import com.su.kddq.utils.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;


/**
 * 用户controller
 *
 * @author sx
 */
@RestController
@RequestMapping("/hddq/express/h5/address/")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    /**
     * 获取当前用户信息
     *
     * @return
     */
    @GetMapping({"getUserInfo"})
    public R getUserInfo() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Object> userInfo = userService.getUserInfo(username);
        return R.ok().data("userinfo", userInfo);
    }

    /**
     * 阿里云oss
     *
     * @param file
     * @return
     */
    @PostMapping({"upfile"})
    public R UpUserAvatar(@RequestParam("useravatar") MultipartFile file) {
        String url = userService.UpUserAvatar(file);
        return R.ok().data("url", url);
    }


    /**
     * 发送阿里云短信
     *
     * @param phone
     * @return
     */
    @GetMapping({"sendmsm/{phone}"})
    public R sendMsm(@PathVariable String phone) {
        return userService.sendMsm(phone) ? R.ok() : R.error();
    }

    /**
     * 注册
     *
     * @param userRegister
     * @return
     */
    @PostMapping({"register"})
    public R register(@RequestBody UserRegister userRegister) {
        boolean isRegister = userService.register(userRegister);
        return isRegister ? R.ok() : R.error();
    }

    /**
     * token续期
     *
     * @param dataMap
     * @return
     */
    @PostMapping({"refreshtoken"})
    public R reFreshToken(@RequestBody Map dataMap) {
        Map<String, String> map = userService.reFreshToken(dataMap);
        return map.get("isRefresh").equals("true") ? R.ok().data("token", map.get("token")).data("reFresh", map.get("reFresh")) : R.error().code(ResultCode.TOKEN_FAIL);
    }

    /**
     * 用户支付订单
     *
     * @param order
     * @return
     */
    @PostMapping({"payorder"})
    public R payOrder(@RequestBody Order order) {
        boolean isPay = this.orderService.payOrder(order);
        return isPay ? R.ok() : R.error();
    }

    /**
     * 用户充值调用支付宝
     *
     * @return
     */
    @GetMapping({"topay/{money}"})
    public R toPay(@PathVariable String money) {
        Map<String, Object> payMap = userService.toPay(money);
        Object isPay = payMap.get("isPay");
        return isPay.equals(true) ? R.ok().data(payMap) : R.error();
    }

    /**
     * @return 查询用户钱包
     */
    @GetMapping({"querywallet"})
    public R queryWallet() {
        UserWallet userWallet = userService.queryWallet();
        return R.ok().data("userWallet", userWallet);
    }

    /**
     * 取消订单
     *
     * @param map
     * @return
     */
    @PutMapping({"cancelorder"})
    public R cancelOrder(@RequestBody Map map) {
        boolean isCancel = this.orderService.cancelOrder(map);
        return isCancel ? R.ok() : R.error();
    }

    /**
     * 用户登录将用户id绑定设备cid
     *
     * @param cid
     * @return
     */
    @GetMapping({"bindCid/{cid}"})
    public R bindCid(@PathVariable(name = "cid") String cid) {
        String uid = SecurityContextHolder.getContext().getAuthentication().getName();
        userService.saveOrUpdate(null, new UpdateWrapper<User>().set("cid", cid).eq("id", uid));
        return R.ok().data("cid", cid);
    }

    /**
     * 检查版本更新
     *
     * @return
     */
    @GetMapping({"checkUpdate/{version}"})
    public R checkUpdate(@PathVariable String version) {
        Map map = userService.checkUpdate(version);
        Object isUpdate = map.get("isUpdate");
        return isUpdate.equals(true) ? R.ok().data("versionInfo", map.get("version_info")) : R.ok().code((Integer) map.get("code")).data("versionInfo", map.get("version_info"));
    }


    /**
     * 查询用户充值流水
     *
     * @return
     */
    @GetMapping({"queryPayOrder/{pageNum}/{pageSize}"})
    public R queryPayOrder(@PathVariable(value = "pageNum") Integer pageNum,
                           @PathVariable(value = "pageSize") Integer pageSize) {
        IPage<PayOrder> payOrderIPage = userService.queryPayOrder(pageNum, pageSize);
        return R.ok().data("payOrderIPage", payOrderIPage);
    }

}